using System;
using FireStarterModeller.BO;
using FireStarterModeller.Logic.Wizards;
using Habanero.Base;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// This control is used to managed all the <see cref="DMClass"/>s and all their details.
    /// This controls effectively binds the DMClass Selector with the DMClassEditor where the 
    /// DMClassEditor is an editor for editing
    /// <li>The attributes that are defined for the <see cref="DMClass"/> e.g. ClassName, ClassDescription </li>"    
    /// <li>The Properties that are defined for the <see cref="DMClass"/> </li>"    
    /// <li>The Relationships that are defined for the <see cref="DMClass"/> i.e. how this class is related to other classes</li>"    
    /// <li>The Unique constratins that are defined for the <see cref="DMClass"/> i.e. any alternate keys </li>"    
    ///These are all implemented in a <see cref="CollapsiblePanelGroupControlWin"/>.  
    /// </summary>
    public class DMClassAllDetailsControl : UserControlWin, IFormControl, IBusinessObjectControl
    {
        private readonly DMSolutionControl _dmSolutionControl;

        private readonly IControlFactory _controlFactory;
        private readonly DMClassDetailEditorControl _editorControl;
        
        public DMClassAllDetailsControl(IControlFactory controlFactory, IFireStarterFormsController formsController)
        {
            _controlFactory = controlFactory;
            _dmSolutionControl = new DMSolutionControl(_controlFactory) {Width = 200};
            _editorControl = new DMClassDetailEditorControl(_controlFactory, formsController);
           
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(_dmSolutionControl, BorderLayoutManager.Position.West, true);
            layoutManager.AddControl(_editorControl, BorderLayoutManager.Position.Centre);
            new BOSelectorAndEditorManager(_dmSolutionControl, _editorControl);
            new BOSelectorAndEditorManager(_dmSolutionControl.DMClassGridSelectorControl, _editorControl);
            _dmSolutionControl.DeleteClicked += DeleteClickedHandler;
            _dmSolutionControl.CancelClicked += CancelClickedHandler;
        }

        public IControlFactory ControlFactory
        {
            get { return _controlFactory; }
        }

        public DMSolution Solution
        {
            get { return (DMSolution)_dmSolutionControl.BusinessObject; }
            set
            {
                this._editorControl.Solution = value;
                _dmSolutionControl.BusinessObject = value;
                this.Enabled = (value != null);
            }
        }

        public DMClassDetailEditorControl EditorControl
        {
            get { return _editorControl; }
        }

        public IBOColSelectorControl DMSolutionControl
        {
            get { return _dmSolutionControl; }
        }

        public void SetForm(IFormHabanero form)
        {
            if (form == null) return;
            form.Closed += delegate 
            {
                //((DMSolutionControl)this.DMSolutionControl).CleanUp();
            };
        }

        public IBusinessObject BusinessObject
        {
            get { return this.Solution; }
            set
            {
                this.Solution = (DMSolution) value;
            }
        }

        private void CancelClickedHandler(object sender, BOEventArgs e)
        {

            try
            {
                DMClass dmClass = (DMClass)e.BusinessObject;
                if (dmClass != null)
                {
                    this.EditorControl.DMClass = dmClass;
                }
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

        private void DeleteClickedHandler(object sender, EventArgs e)
        {
            try
            {
                DMClass dmClass = this._editorControl.DMClass;
                if (dmClass == null) return;
                DialogResult dialogResult = ControlFactory.ShowMessageBox("Are you certain you want to delete the Entity '" + dmClass.ToString() + "'",
                                                                          "Delete Entity", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    //TODO brett 11 Sep 2010: Move this to use the ClassDeleter wizard.
                    new DMClassDeleter(dmClass).DeleteAll();
                }
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }
    }

}